﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DoubleH.Utility;
using Table = FCNS.Data.Table;
using System.Collections;

namespace ReportS
{
    /// <summary>
    /// 收支明细.xaml 的交互逻辑
    /// </summary>
    public partial class 收支明细表 : Window
    {
        bool noPay=true;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="noPay">true 应收支  false 已所有收支</param>
        public 收支明细表(bool noPay)
        {
            InitializeComponent();

            this.noPay=noPay;
            InitVar();
            InitEvent();
        }

        private void InitVar()
        {
            uCdataGrid1.TableText =noPay?DataTableText.应收支明细表: DataTableText.收支明细表;
            checkComboBoxFlag.ItemsSource = Enum.GetNames(typeof(Table.PayS.EnumFlag));

            checkComboBoxBank.ItemsSource = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.支付方式);
            checkComboBoxBank.DisplayMemberPath = "Name";
            checkComboBoxBank.ValueMemberPath = "Id";

            uCCorS1.Init();
        }

        private void InitEvent()
        {
            buttonOK.Click += (ss, ee) =>
            {
                StringBuilder sb = new StringBuilder(" where PayS.Enable="+(noPay?0:1));
                if (noPay)
                {
                    sb.Append(" and cast(PayS.RelatedOrderDateTime as varchar)>='" + uCDateTime1.StartDateTimeString + "'");
                    sb.Append(" and cast(PayS.RelatedOrderDateTime as varchar)<='" + uCDateTime1.EndDateTimeString + "'");
                }
                else
                {
                    sb.Append(" and cast(PayS.OrderDateTime as varchar)>='" + uCDateTime1.StartDateTimeString + "'");
                    sb.Append(" and cast(PayS.OrderDateTime as varchar)<='" + uCDateTime1.EndDateTimeString + "'");
                }
                sb.Append(MainWindowFunciton.FormatCheckComboBoxValue(checkComboBoxBank, "PayS.BankId"));
                sb.Append(MainWindowFunciton.FormatCheckComboBoxValue(checkComboBoxFlag, "PayS.Flag"));
                sb.Append(MainWindowFunciton.FormatCheckComboBoxValue(uCCorS1, "PayS.CorSId"));
              
                List<TempFunction> tf = new List<TempFunction>();
                Table.PayS ps = new Table.PayS();
                if (noPay)
                    NoPayData(tf, ps.GetListByWhere(sb.ToString()));
                else
                    PayData(tf, ps.GetListByWhere(sb.ToString()));

                uCdataGrid1.Init(tf, new UC.CharProperty { Series = new System.Windows.Controls.DataVisualization.Charting.LineSeries(), ValueTitle = "收入", ValueName= "MoneyIn" },
                    new UC.CharProperty { Series = new System.Windows.Controls.DataVisualization.Charting.LineSeries(), ValueTitle = "支出", ValueName = "MoneyOut" });
            };
        }

        private void NoPayData(List<TempFunction> tf, IList ps)
        {
            foreach (Table.PayS p in ps)
            {
                if (p.Money == 0)//没钱收的单就不显示了
                    continue;

                TempFunction t = new TempFunction();
                t.OrderDateTime = p.RelatedOrderDateTime;
                t.Note = p.Note;
                t._CorSName = p._CorSName;
                t._BankName = p._BankName;

                if (p.Flag == Table.PayS.EnumFlag.付款单 || p.Flag == Table.PayS.EnumFlag.其它费用支出单)
                    t.MoneyOut = -p.Money;
                else
                    t.MoneyIn = p.Money;

                if (string.IsNullOrEmpty(p.RelatedOrderNO))
                    t.Source = "其它收支款";
                else
                {
                    switch (p.RelatedOrderNO.Substring(0, 2))
                    {
                        case "CG":
                            t.Source = "采购款"; break;
                        case "PF":
                            t.Source = "批发款"; break;
                        //case "PO":
                        //    t.Source = "零售款";break;
                        case "SH":
                            t.Source = "售后款"; break;
                    }
                }

                tf.Add(t);
            }
        }

        private void PayData(List<TempFunction> tf,IList  ps)
        {
            foreach (Table.PayS p in ps)
            {
                //if (p.RealMoney == 0)//没钱收的单就不显示了
                //    continue;

                TempFunction t = new TempFunction();
                t.OrderDateTime = p.OrderDateTime.Value;
                t.Note = p.Note;
                t._CorSName = p._CorSName;
                t._BankName = p._BankName;

                if (p.Flag == Table.PayS.EnumFlag.付款单 || p.Flag == Table.PayS.EnumFlag.其它费用支出单)
                {
                    t.Money = -p.Money;
                    t.MoneyOut = -p.RealMoney;
                }
                else
                {
                    t.Money = p.Money;
                    t.MoneyIn = p.RealMoney;
                }

                if (string.IsNullOrEmpty(p.RelatedOrderNO))
                    t.Source = "其它收支款";
                else
                {
                    switch (p.RelatedOrderNO.Substring(0, 2))
                    {
                        case "CG":
                            t.Source = "采购款"; break;
                        case "PF":
                            t.Source = "批发款"; break;
                        //case "PO":
                        //    t.Source = "零售款";break;
                        case "SH":
                            t.Source = "售后款"; break;
                    }
                }

                tf.Add(t);
            }
        }

        class TempFunction
        {
            public DateTime OrderDateTime { get; set; }
            public double? MoneyIn { get; set; }
            public double? MoneyOut { get; set; }
            public string Note { get; set; }
            public string Source { get; set; }
            public double? Money { get; set; }
            public string _CorSName { get; set; }
            public string _BankName { get; set; }
        }
    }
}
